﻿using System.Collections.Generic;
using Game.UI;
using UnityEngine;
using ZYLFramework.Common;
using ZYLFramework.UI;
using Object = System.Object;

namespace ZYLFramework.Manage
{
    public class UIConfig
    {
        public UIEnum UId;
        public string Module;
        public GameObject Prefab;
        public UIModel Model;
        public UICtrl Ctrl;
        public UIView View;
    }
    
    public class UIManage:MonoSingleton<UIManage>
    {
        //UI配置信息列表
        public List<UIConfig> UIConfigs;
        // UI的面板名和层级
        public Dictionary<UIEnum, int> UILayer;

        // UI的面板和配置映射
        public Dictionary<UIEnum, int> UIDict;

        private Dictionary<UIEnum,GameObject> UIGameObject;
        //UI栈表
        private Stack<UIEnum> UIStack;
        //当前激活UI
        public UIConfig ActiveUIConfig;

        private GameObject RootPanel;

        private GameObject MainCanvas;
        private GameObject UICamera;

        public UIManage()
        {
            UIConfigs = new List<UIConfig>();
            UILayer = new Dictionary<UIEnum, int>();
            UIDict = new Dictionary<UIEnum, int>();
            UIGameObject = new Dictionary<UIEnum, GameObject>();
            UIStack = new Stack<UIEnum>();
        }

        protected override void Awake()
        {
            base.Awake();
            MainCanvas = GameObject.Find("Canvas");
            UICamera = GameObject.Find("Canvas/UICamera");
            ActiveUIConfig = null;
        }

        public void Register(UIEnum uiName,string module,UICtrl ctrl,UIModel model,UIView view)
        {
            GameObject item;
            Tool.Tool.NewUIGameObject("UI/"+module, MainCanvas, out item);
            item.SetActive(false);
            UIConfig temp = new UIConfig
            {
                UId = uiName,
                Module = module,
                Prefab = item,
                Model = model,
                Ctrl = ctrl,
                View = view,
            };
            temp.View.BaseInit(item);
            temp.Model.BaseInit();
            temp.Ctrl.BaseInit(temp.Model,temp.View);
            UIDict.Add(uiName,UIConfigs.Count);
            UIConfigs.Add(temp);
        }
        
        /// <summary>
        /// 在显示层上显示一个ui面板，如果已经显示了不会重复打开
        /// </summary>
        /// <param name="uiName"></param>
        /// <param name="objects"></param>
        public void Open(UIEnum uiName,params Object[] objects)
        {
            var uiInfo = GetUIInfo(uiName);
            if (uiInfo == null)
            {
                return;
            }

            if (ActiveUIConfig != null)
            {
                // 如果已经打开了
                if (ActiveUIConfig.UId == uiName)
                {
                    return;
                }
                UIHide(ActiveUIConfig.UId);
                UIStack.Push(ActiveUIConfig.UId);
            }
            ActiveUIConfig = uiInfo;
            UIShow(ActiveUIConfig.UId,objects);
        }
        
        public void Close()
        {
            if (ActiveUIConfig == null)
            {
                return;
            }

            var uiName = ActiveUIConfig.UId;
            var uiInfo = GetUIInfo(uiName);
            if (uiInfo == null)
            {
                return;
            }
            UIHide(uiName);
            if (UIStack.Count > 0)
            {
                var stackUIName = UIStack.Pop();
                ActiveUIConfig = GetUIInfo(stackUIName);
                UIShow(ActiveUIConfig.UId);
            }
            else
            {
                ActiveUIConfig = null;
            }
        }
        
        public void UIShow(UIEnum uiName,params object[] objects){
            var uiInfo = GetUIInfo(uiName);
            if (uiInfo == null)
            {
                return;
            }
            uiInfo.Prefab.SetActive(true);
            uiInfo.Ctrl.Show(objects);
            Tool.Tool.SetVisible(uiInfo.Prefab,true);
        }
        
        public void UIHide(UIEnum uiName){
            var uiInfo = GetUIInfo(uiName);
            if (uiInfo == null)
            {
                return;
            }
            Tool.Tool.SetVisible(uiInfo.Prefab,false);
        }
        
        private UIConfig GetUIInfo(UIEnum uiName){
            if (!UIDict.ContainsKey(uiName))
            {
                return null;
            }

            var uiInfoIndex = UIDict[uiName];
            var uiInfo = UIConfigs[uiInfoIndex];
            return uiInfo;
        }
    }
}